/*!
 * vinculum
 * Copyright(c) 2017 azzly
 * MIT Licensed
 */
 
'use strict';

const table = require('./table');
const column = require('./column');
const CONST = require("./CONST");

const TABLE_PREFIX = CONST.TABLE_PREFIX;

let tables = [];

function copySetName(column,newname,def){
  let copyobj = Object.assign({},column);
  copyobj.name = newname;
  if(def || def ===0) copyobj.def = def;
  return copyobj;
}

let varchar20 = new column('varchar20','varchar',20,null);
let varchar50 = new column('varchar50','varchar',50,null);
let varchar100 = new column('varchar100','varchar',100,null);
let varchar255 = new column('varchar255','varchar',255,null);
let timestamp = new column('timestamp','timestamp',0,'current_timestamp');
let tinyint = new column('tinyint','tinyint',4,0);
let integer = new column('integer','int',11,0);
let text = new column('text','text',0,null);
let datetime = new column('dt','datetime',0,null);

let id = copySetName(varchar50,'id');
let createtime = copySetName(timestamp,'createtime');
let isfilter = copySetName(tinyint,'isfilter');
let islog = copySetName(tinyint,'islog');
let name = copySetName(varchar255,'name');
let title = copySetName(varchar255,'title');
let authurl = copySetName(varchar255,'authurl');
let l10n = copySetName(varchar255,'l10n');
let main = copySetName(text,'main');
let locktype = copySetName(varchar20,'locktype');
let appid = copySetName(varchar50,'appid');
let resourceid = copySetName(varchar50,'resourceid');

/* 动作表 */
let action = new table(`${TABLE_PREFIX}_action`);
action.addColumn(id);
action.addColumn(name);
action.addColumn(l10n);
action.addColumn(copySetName(text,'main'));
action.addColumn(isfilter);
action.addColumn(copySetName(tinyint,'disabled'));
action.addColumn(islog);
action.addColumn(locktype);
action.addColumn(createtime);
action.setPrimaryKey('id');

/* 关联表 */
let linker = new table(`${TABLE_PREFIX}_linker`);
linker.addColumn(copySetName(varchar50,'mainid'));
linker.addColumn(copySetName(varchar50,'linkid'));
linker.addColumn(copySetName(varchar50,'linktype'));
linker.addColumn(copySetName(integer,'ordernum'));
linker.addColumn(createtime);

/* 应用表 */
let application = new table(`${TABLE_PREFIX}_application`);
application.addColumn(id);
application.addColumn(name);
application.addColumn(title);
application.addColumn(isfilter);
application.addColumn(islog);
application.addColumn(authurl);
application.addColumn(copySetName(varchar255,'description'));
application.addColumn(copySetName(tinyint,'debug'));
application.addColumn(copySetName(tinyint,'disabled'));
application.addColumn(copySetName(varchar255,'keywords'));
application.addColumn(copySetName(varchar255,'viewport',"'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no'"));
application.addColumn(copySetName(varchar50,'before_service'));
application.addColumn(copySetName(varchar50,'before_action'));
application.addColumn(copySetName(text,'main_html'));
application.addColumn(copySetName(text,'main_js'));
application.addColumn(copySetName(text,'main_css'));
application.addColumn(createtime);
application.setPrimaryKey('id');

/* 权限表 */
let authorization = new table(`${TABLE_PREFIX}_authorization`);
authorization.addColumn(id);
authorization.addColumn(copySetName(varchar50,'roleid'));
authorization.addColumn(copySetName(varchar50,'whattype'));
authorization.addColumn(copySetName(varchar50,'whatid'));
authorization.addColumn(copySetName(varchar50,'authtype'));
authorization.addColumn(createtime);
authorization.setPrimaryKey('id');

/* 定时任务表 */
let cron = new table(`${TABLE_PREFIX}_cron`);
cron.addColumn(id);
cron.addColumn(copySetName(varchar100,'cron_time'));
cron.addColumn(copySetName(varchar100,'action_name'));
cron.addColumn(copySetName(varchar255,'cron_name'));
cron.addColumn(copySetName(varchar100,'service_name'));
cron.addColumn(copySetName(varchar50,'excute_type'));
cron.addColumn(copySetName(varchar255,'excute_params'));
cron.addColumn(copySetName(tinyint,'isrun'));
cron.addColumn(islog);
cron.addColumn(createtime);
cron.setPrimaryKey('id');

/* 数据源表 */
let datasource = new table(`${TABLE_PREFIX}_datasource`);
datasource.addColumn(id);
datasource.addColumn(copySetName(varchar50,'name'));
datasource.addColumn(copySetName(varchar50,'type'));
datasource.addColumn(copySetName(varchar50,'host'));
datasource.addColumn(copySetName(varchar50,'user'));
datasource.addColumn(copySetName(varchar50,'password'));
datasource.addColumn(copySetName(varchar50,'database'));
datasource.addColumn(copySetName(tinyint,'climit'));
datasource.addColumn(createtime);
datasource.setPrimaryKey('id');

/* 执行日志表 */
let log = new table(`${TABLE_PREFIX}_log`);
log.addColumn(id);
log.addColumn(copySetName(varchar50,'logname'));
log.addColumn(copySetName(varchar20,'type'));
log.addColumn(copySetName(varchar50,'request_ip'));
log.addColumn(copySetName(varchar50,'userid'));
log.addColumn(copySetName(text,'request_params'));
log.addColumn(copySetName(text,'response_params'));
log.addColumn(createtime);
log.setPrimaryKey('id');

/* 参数表 */
let params = new table(`${TABLE_PREFIX}_params`);
params.addColumn(id);
params.addColumn(copySetName(varchar50,'name'));
params.addColumn(copySetName(varchar50,'l10n'));
params.addColumn(copySetName(varchar20,'type'));
params.addColumn(createtime);
params.setPrimaryKey('id');

/* 资源表 */
let resource = new table(`${TABLE_PREFIX}_resource`);
resource.addColumn(id);
resource.addColumn(copySetName(varchar50,'name'));
resource.addColumn(copySetName(varchar20,'version'));
resource.addColumn(copySetName(tinyint,'type'));
resource.addColumn(copySetName(text,'main'));
resource.addColumn(copySetName(varchar20,'filetype'));
resource.addColumn(copySetName(integer,'ordernum'));
resource.addColumn(createtime);
resource.setPrimaryKey('id');

/* 角色表 */
let role = new table(`${TABLE_PREFIX}_role`);
role.addColumn(id);
role.addColumn(copySetName(varchar50,'name'));
role.addColumn(copySetName(varchar50,'l10n'));
role.addColumn(createtime);
role.setPrimaryKey('id');

/* 服务表 */
let service = new table(`${TABLE_PREFIX}_service`);
service.addColumn(id);
service.addColumn(copySetName(varchar50,'name'));
service.addColumn(copySetName(varchar50,'l10n'));
service.addColumn(copySetName(text,'main'));
service.addColumn(copySetName(tinyint,'type'));
service.addColumn(copySetName(varchar50,'datasourceid',"'systemdb'"));
service.addColumn(isfilter);
service.addColumn(islog);
service.addColumn(copySetName(tinyint,'disabled'));
service.addColumn(createtime);
service.setPrimaryKey('id');

/* 第三方信息表 */
let third_party = new table(`${TABLE_PREFIX}_third_party`);
third_party.addColumn(copySetName(varchar50,'appid'));
third_party.addColumn(copySetName(varchar50,'appsecret'));
third_party.addColumn(copySetName(varchar50,'l10n'));
third_party.addColumn(copySetName(datetime,'starttime'));
third_party.addColumn(copySetName(datetime,'endtime'));
third_party.addColumn(createtime);

/* 第三方信息授权表 */
let third_party_auth = new table(`${TABLE_PREFIX}_third_party_auth`);
third_party_auth.addColumn(copySetName(varchar50,'appid'));
third_party_auth.addColumn(copySetName(varchar50,'whatid'));
third_party_auth.addColumn(copySetName(varchar50,'whattype'));
third_party_auth.addColumn(createtime);

/* 用户表 */
let user = new table(`${TABLE_PREFIX}_user`);
user.addColumn(id);
user.addColumn(copySetName(varchar50,'username'));
user.addColumn(copySetName(varchar50,'userpwd'));
user.addColumn(createtime);
user.setPrimaryKey('id');

module.exports = {
  action:action
  ,linker:linker
  ,application:application
  ,authorization:authorization
  ,cron:cron
  ,datasource:datasource
  ,log:log
  ,params:params
  ,resource:resource
  ,role:role
  ,service:service
  ,third_party:third_party
  ,third_party_auth:third_party_auth
  ,user:user
};